package gw.com.android.ui.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.gwtsz.android.rxbus.RxBus;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.GTConfig;
import gw.com.android.greendao.NotifyDataHelper;
import gw.com.android.model.DataManager;
import gw.com.android.presenter.LoginPresenter;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.MainActivity;
import gw.com.android.ui.WebFragment;
import gw.com.android.utils.PackStatis;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.app.ObjectSessionStore;
import www.com.library.view.TintTextView;

/**
 * Created by ari on 2017/9/19.
 */
public class LogOutDialog extends DialogFragment {
    TintTextView mTv_sure, mTv_cancel;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final Window window = getDialog().getWindow();
        View view = inflater.inflate(R.layout.dialog_fragment_ext, ((ViewGroup) window.findViewById(android.R.id.content)), false);//需要用android.R.id.content这个view
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//注意此处
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mTv_sure = (TintTextView) view.findViewById(R.id.action_btn_pos);
        mTv_cancel = (TintTextView) view.findViewById(R.id.action_btn_neg);
        mTv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GTConfig.instance().setBooleanValue(GTConfig.PREF_AUTO_LOGIN, false);

                GTConfig.instance().setAccountType(GTConfig.ACC_TYPE_GUEST);
                GTConfig.instance().mLastAccountType = GTConfig.ACC_TYPE_GUEST;
                GTConfig.instance().isPhoneLogin = false;
//					GTConfig.instance().phoneLoginInfo ="";
                GTConfig.instance().mCurLoginPhone = "";
                GTConfig.instance().mCurName = "";
                GTConfig.instance().mUserPwd = "";

                final MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
                if (null != mainActivity) {
                    mainActivity.removeAllFragment(false, true);
                }
                WebFragment.clearCookie();
                DataManager.instance().reset();
                LoginPresenter presenter = new LoginPresenter(getActivity());
                presenter.guestLogin();
                GTConfig.instance().setJpushAliasAndTags(null, "");
                PackStatis.getToService(PackStatis.EventAction.LOGOUT.getValue(), PackStatis.EventCategory.MAIN.getValue(), null, null);
                getActivity().finish();
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (mainActivity != null) {
                            mainActivity.showMeFragment();
                        }
                        NotifyDataHelper.notifyChange();
                    }
                });
                RxBus.getInstance().post(GTSConst.REPLY_SWITCH_LOGIN_TYPE, GTConfig.instance().getAccountType());
            }
        });
        mTv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogOutDialog.this.dismiss();
            }
        });
    }

}
